package com.itheima.ydd.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.IYddUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_user")
public class YddUserController {
    @Autowired
    private IYddUserService yddUserService;

    /**
     * 用户列表
     * @param user
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public R userList(YddUser user, String page, String limit){
        Page<YddUser> userPage = new Page<>(Long.parseLong(page),Long.parseLong(limit));
        LambdaQueryWrapper<YddUser> userLqw = new LambdaQueryWrapper<>();
        userLqw.like(user.getSex()!=null,YddUser::getSex,user.getSex())
                .like(user.getUsername()!=null,YddUser::getUsername,user.getUsername())
                .like(user.getMobile()!=null,YddUser::getMobile,user.getMobile())
                .orderByDesc(YddUser::getId);
        yddUserService.page(userPage,userLqw);
        R r = R.success();
        r.putData("count",userPage.getTotal());
        r.putData("totalPages",userPage.getPages());
        r.putData("pageSize",userPage.getSize());
        r.putData("currentPage",userPage.getCurrent());
        r.putData("data",userPage.getRecords());
        return r;
    }

    /**
     * 添加用户
     * @param yddUser
     * @return
     */
    @PostMapping("/add")
    public R userAdd(@RequestBody YddUser yddUser){
        String password = DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes());
        yddUser.setPassword(password);
        boolean result = yddUserService.save(yddUser);
        if (result){
            return R.success();
        }
        return R.error("添加失败");
    }

    /**
     * 用户详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R rowInfo(Long id){
        YddUser yddUser = yddUserService.getById(id);
        yddUser.setPassword(null);
        if (yddUser==null){
            return R.error("用户不存在,请刷新后重试");
        }
        HashMap map = JSON.parseObject(JSON.toJSONString(yddUser), HashMap.class);
        R r = R.success();
        r.setData(map);
        return r;
    }

    /**
     * 修改用户数据
     * @param user
     * @return
     */
    @PostMapping("/update")
    public R userUpdate(@RequestBody YddUser user){
        if (user.getPassword()!=null){
            String password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
            user.setPassword(password);
        }
        boolean result = yddUserService.updateById(user);
        if (result){
            return R.success();
        }
        return R.error("修改失败");
    }

    /**
     * 删除用户数据
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R userDel(String id){
        boolean result = yddUserService.removeById(id);
        if (result){
            return R.success();
        }
        return R.error("删除失败");
    }




}
